package b.Servlet;

import b.DAO.Impl.UserDAOImpl;
import b.DAO.UserDAO;
import b.domain.User;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

@WebServlet("/ServletRequetDemo1")
public class ServletRequetDemo1 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
//        String username = request.getParameter("username");
//        String password = request.getParameter("password");
//        String email = request.getParameter("email");
//        String name = request.getParameter("name");
//        String gender = request.getParameter("gender");
//        String birthday = request.getParameter("birthday");
        Map<String,String[]> map = request.getParameterMap();
//        JdbcTemplate template = new JdbcTemplate(DruidUtils.getDataSource());
//        String sql ="insert into user values(null,?,?,?,?,?,?)";
//        template.update(sql,username,password,email,name,gender,birthday);
        User user = new User();
        //使用BeanUtils封装JavaBean数据
        //需要一个map集合，也是请求参数返回的map集合
        try {
            BeanUtils.populate(user,map);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        }
        System.out.println(user);
        //调用DAO方法
        UserDAO userDAO = new UserDAOImpl();
        userDAO.insertUser(user);
    }
}
